package com.xbongbong.saas.script.tempenum.product;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkKeyFieldInfoPojo;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.AssembleEnum;
import com.xbongbong.saas.enums.product.AssembleProductEnum;
import com.xbongbong.saas.enums.product.OutstockProductEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/10/12 10:32
 * @since v1.0
 */
@Getter
public enum TempAssembleProductEnum {
    PRODUCT(AssembleProductEnum.PRODUCT),
    WAREHOUSE(AssembleProductEnum.WAREHOUSE),
    UNIT(AssembleProductEnum.UNIT),
    NUM(AssembleProductEnum.NUM),
    STOCK(AssembleProductEnum.STOCK),
    COST(AssembleProductEnum.COST),
    TOTAL_COST(AssembleProductEnum.TOTAL_COST),
    MEMO(AssembleProductEnum.MEMO),
    BATCH(AssembleProductEnum.BATCH),
    PRODUCE_DATE(AssembleProductEnum.PRODUCE_DATE),
    GUARANTEE_PERIOD(AssembleProductEnum.GUARANTEE_PERIOD),
    EXPIRE_DATE(AssembleProductEnum.EXPIRE_DATE),
    ;

    private AssembleProductEnum assembleProductEnum;

    TempAssembleProductEnum(AssembleProductEnum assembleProductEnum) {
        this.assembleProductEnum = assembleProductEnum;
    }

    public static void initProduct(FieldAttrEntity fieldAttrEntity, List<LinkedAttrPoJo> linkedAttrPoJoList,List<LinkedAttrPoJo> linkedShowPoJoList, PaasFormEntityExt paasFormEntity){
        LinkInfoPojo linkInfoPojo = new LinkInfoPojo();
        linkInfoPojo.setLinkBusinessType(XbbRefTypeEnum.PRODUCT.getCode());
        linkInfoPojo.setLinkedAttr(linkedAttrPoJoList);
        linkInfoPojo.setLinkSaasMark(SaasMarkEnum.SAAS.getCode());
        linkInfoPojo.setLinkKey(linkedAttrPoJoList.get(1).getAttr());
        linkInfoPojo.setLinkFormId(paasFormEntity.getId());
        linkInfoPojo.setLinkMenuId(paasFormEntity.getMenuId());
        linkInfoPojo.setLinkAppId(paasFormEntity.getAppId());
        linkInfoPojo.setCondition(new ArrayList<>());
        LinkKeyFieldInfoPojo linkKeyFieldInfoPojo = new LinkKeyFieldInfoPojo();
        BeanUtil.copyProperties(linkedAttrPoJoList.get(1),linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkKeyAttr(linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkedShowAttr(linkedShowPoJoList);
        fieldAttrEntity.setLinkInfo(linkInfoPojo);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias()));
    }

    // 初始化产品中的其他属性
    public static void initWarehouse(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PRODUCT_WAREHOUSE.getCode());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(OutstockProductEnum.PRODUCT.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.PRODUCT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_CORRELATION_FIELD.getAlias()));
    }

    public static void initCost(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initNum(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultValue(1);
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setShowEditable(1);
    }

    public static void initStock(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initUnit(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_ITEMS.getAlias(),
                ForbiddenSettingEnum.DISABLE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }
    public static void initProductDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initGuaranteePeriod(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIntegerOnly(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),ForbiddenSettingEnum.DISABLE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setEditable(0);
    }

    public static void initMemo(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setIsForbidden(1);
    }

    public static void initBatch(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_TYPE.getAlias()));
    }

    public static void initTotalCost(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.COST.getAttr() + "}*{self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.COST.getAttr(), "关联产品.成本");
        labelMap.put("self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr(), "关联产品.数量");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initExpireDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr() + "}+24*60*60*{self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr(), "关联产品.生产日期");
        labelMap.put("self." + AssembleEnum.OUT_PRODUCTS.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr(), "关联产品.保质期");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }
}
